package com.gewu.pm.ui.fragment;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gewu.pm.R;
import com.gewu.pm.aop.SingleClick;
import com.gewu.pm.app.AppAdapter;
import com.gewu.pm.app.TitleBarFragment;
import com.gewu.pm.bean.MeItemBean;
import com.gewu.pm.ui.MainActivity;
import com.gewu.pm.ui.activity.AboutActivity;
import com.gewu.pm.ui.activity.LogoutAccountActivity;
import com.gewu.pm.ui.activity.PasswordResetActivity;
import com.gewu.pm.ui.activity.PersonalDataActivity;
import com.gewu.pm.ui.activity.account.SwitchAccountActivity;
import com.gewu.pm.ui.adapter.MeItemAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的 Fragment
 */
public final class MeFragment extends TitleBarFragment<MainActivity> implements AppAdapter.OnItemClickListener {
    private RecyclerView recyclerview;
    private ImageView imgHead;//头像
    private TextView tvNotLogin;//未登录
    private LinearLayout layoutPersonal;//已登录
    private TextView tvName;//姓名
    private TextView tvCompany;//公司
    private TextView tvPhone;//手机号

    private MeItemAdapter meItemAdapter;
    private List<MeItemBean> mData;


    public static MeFragment newInstance() {
        return new MeFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.me_fragment;
    }

    @Override
    protected void initView() {
        recyclerview = findViewById(R.id.recyclerview);
        imgHead = findViewById(R.id.img_head);
        tvNotLogin = findViewById(R.id.tv_not_login);
        layoutPersonal = findViewById(R.id.layout_personal);
        tvName = findViewById(R.id.tv_name);
        tvCompany = findViewById(R.id.tv_company);
        tvPhone = findViewById(R.id.tv_phone);

        setOnClickListener(imgHead);

    }

    @Override
    protected void initData() {
        mData = new ArrayList<>();
        initRecyclerview();

    }

    @SingleClick
    @Override
    public void onClick(View view) {
        int viewId = view.getId();
        if (viewId == R.id.img_head){
            startActivity(PersonalDataActivity.class);
        }

    }

    /**
     * 功能标签
     */
    private void initRecyclerview() {
        mData.add(new MeItemBean("密码修改", R.drawable.me_password, 1));
        mData.add(new MeItemBean("注销账号", R.drawable.me_logout, 2));
        mData.add(new MeItemBean("切换账号", R.drawable.me_switchover, 3));
        mData.add(new MeItemBean("关于我们", R.drawable.me_us, 4));

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerview.setLayoutManager(layoutManager);

        meItemAdapter = new MeItemAdapter(getAttachActivity());
        meItemAdapter.setOnItemClickListener(this);
        recyclerview.setAdapter(meItemAdapter);
        meItemAdapter.setData(mData);

    }

    @Override
    public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
        MeItemBean bean = meItemAdapter.getItem(position);
        int type = bean.getType();
        if (type == 1) {//密码修改
            startActivity(PasswordResetActivity.class);

        } else if (type == 2) {//注销账号
            startActivity(LogoutAccountActivity.class);

        } else if (type == 3) {//切换账号
            // 跳转到切换账号页面，传递当前账号ID
            SwitchAccountActivity.start(getContext(), "1");

        } else if (type == 4) {//关于我们
            startActivity(AboutActivity.class);

        }
    }

    @Override
    public boolean isStatusBarEnabled() {
        // 使用沉浸式状态栏
        return !super.isStatusBarEnabled();
    }

}